var myDate = new Date();
var year = myDate.getFullYear();   //获取完整的年份(4位,1970-????)
var month =myDate.getMonth();      //获取当前月份(0-11,0代表1月)
var day = myDate.getDate()+1;       //获取当前日(1-31)
if(month<10)month="0"+month;
if(day<10)day="0"+day;
var tody = year+"-"+month+"-"+day;
var userinfo = null;
$(document).ready(function(){
	cw.common.getUserSession(getUserInfo);//查询用户
	$('#subImg').bind('click',subButton);
	$("#STOCK_DATE").focus(function(){WdatePicker({dateFmt:'yyyy-MM-dd',oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})});
	validate();
	autocompleteFIXED_ASSETS_NAME();//自动提示资产名
	autocompleteSUPPLIERS_NO();//自动提示供应商
	$("#STOCK_DATE").val(tody);
})
var getUserInfo = function(us){
	userinfo=us;
	$("#createBy").html(userinfo.USER_NAME);
	$("#createDate").html(tody);
	queryPERSONNAL();
	//querySUPPLIERS();
	FIXED_ASSETS_NATURE();//查询资产性质
	queryDEPARTMENT();//查询用户管理部门
}
//查询部门
var queryDEPARTMENT = function(){
	$.post(cw.common.path+'/json',
			{
				action : 'QUERY_USER_DEPART_ACTION',
				USER_ID:userinfo.USER_ID
			}, function(data){
				if (data.success) {
					var length = data.root.T_USER_DEPARTMENT.rs.length;
					if(length > 0){
						for(var i=0;i<length;i++) {
							var obj=data.root.T_USER_DEPARTMENT.rs[i];
							$("#GS_DEPARTMENT_NO").append("<option value='"+obj.DEPARTMENT_NO+"'>"+obj.DEPARTMENT_NO_NAME+"</option>");
						}
					}
				} else {
					alert(data.errors.errmsg);
				}
			}, 'json');
}
//查询人员
var queryPERSONNAL = function(){
	$.post(cw.common.path+'/json',
			{
				action : 'QUERY_T_PERSONNAL_ACTION'
			}, function(data){
				if (data.success) {
					var length = data.root.T_PERSONNAL.rs.length;
					if(length > 0){
						for(var i=0;i<length;i++) {
							var obj=data.root.T_PERSONNAL.rs[i];
							$("#WITH_MANANGER").append("<option value='"+obj.PERSONNAL_NO+"'>"+obj.NAME+"</option>");
						}
					}
				} else {
					alert(data.errors.errmsg);
				}
			}, 'json');
}
//自动提示供应商
var autocompleteSUPPLIERS_NO=function(){
	$("#SUPPLIERS_NO").autocomplete(
			encodeURI(cw.common.path+'/json?action=QUERY_T_SUPPLIERS_ACTION&SUPPLIERS_NAME='+$("#SUPPLIERS_NO").val()),
			{
				max: 10,
				scroll: true,
				width: 162,
				dataType: "json",
				//extraParams:{"FIXED_ASSETS_NAME":$("#FIXED_ASSETS_NAME").val()},
				scrollHeight: 150,
				parse: function(data) {
					return $.map(data.root.T_SUPPLIERS.rs, function(row) {
							return {
									data: row,
									value: row.SUPPLIERS_NAME + " <" + row.SUPPLIERS_NO + ">",
									result: row.SUPPLIERS_NAME
									}
							});
				},
				formatItem: function(item) {
					return "<font color=green>" + item.SUPPLIERS_NO + "</font>&nbsp;(" + item.SUPPLIERS_NAME + ")";
				}
			}
			); 
}
//自动提示
var autocompleteFIXED_ASSETS_NAME=function(){
	
	$("#FIXED_ASSETS_NAME").autocomplete(
			encodeURI(cw.common.path+'/json?action=QUERY_T_FIXED_ASSETS_ACTION&FIXED_ASSETS_TYPE=3&FIXED_ASSETS_NAME='+$("#FIXED_ASSETS_NAME").val()),
			{
				max: 10,
				scroll: true,
				width: 162,
				dataType: "json",
				//extraParams:{"FIXED_ASSETS_NAME":$("#FIXED_ASSETS_NAME").val()},
				scrollHeight: 150,
				parse: function(data) {
					return $.map(data.root.T_FIXED_ASSETS.rs, function(row) {
							return {
									data: row,
									value: row.FIXED_ASSETS_NAME + " <" + row.FIXED_ASSETS_NO + ">",
									result: row.FIXED_ASSETS_NAME
									}
							});
				},
				formatItem: function(item) {
					return "<font color=green>" + item.FIXED_ASSETS_NO + "</font>&nbsp;(" + item.FIXED_ASSETS_NAME + ")";
				}
			}
			).result(function(e, item) {
				GET_T_FIXED_ASSETS_ACTION(item.FIXED_ASSETS_NO);
			}); 
}
//取得资产基本信息
var GET_T_FIXED_ASSETS_ACTION = function(id){
	var url = cw.common.path + '/json?action=GET_T_FIXED_ASSETS_ACTION&FIXED_ASSETS_NO='+id;
	$.post(url,{},function(data){
		if(data.success){
			var vobj=data.root.T_FIXED_ASSETS;
			if(vobj==null||vobj==''||vobj==undefined){
				alert("该资产不存在或已被删除");
				return;
			}
			else{
				var obj = data.root.T_FIXED_ASSETS.rs[0];
				$("#FIXED_ASSETS_NO").val(id);
				cw.common.setValue('BRAND',obj.BRAND,'text');
				cw.common.setValue('FIXED_ASSETS_NATURE',obj.FIXED_ASSETS_NATURE,'select');
				cw.common.setValue('FIXED_ASSETS_TYPE',obj.FIXED_ASSETS_TYPE,'select');
				$('#REMARK').val(obj.REMARK);
			}
		}
		else{
			alert(data.errors.errmsg);
		}
	},'json');
}

function subFrm(){
	var fi = $("#PIC").val();
	if(fi==null||fi==''||fi==undefined){
		$("#frm").append('<input type="hidden" name="action" id="action" value="ADD_T_FIXED_ASSETS_DETAIL_ACTION"/>');
	}else{
		$("#frm").append('<input type="hidden" name="ACTION" id="ACTION" value="ADD_T_FIXED_ASSETS_DETAIL_ACTION"/>');
	}
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		$('#subImg').bind('click',subButton);
	    		var vobj=data.root.T_FIXED_ASSETS_DETAIL.rs[0];
	    		alert("添加非耗品成功!");
	    		queryT_FIXED_ASSETS_DETAILLIST(vobj.IDSTR);
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

//资产详细信息
var queryT_FIXED_ASSETS_DETAILLIST = function(id){
	
	var url = cw.common.path + '/json?action=QUERY_T_FIXED_ASSETS_DETAILBYIDBY_PAGER_ACTION';
	url += "&IDSTR="+id;
	var xmlname = "T_FIXED_ASSETS_DETAIL";
	var pagePanel='#T_FIXED_ASSETS_DETAIL_pager';
	var result='#T_FIXED_ASSETS_DETAIL_LIST';
	var pageSize=10;
	var defaultHtml='<li><em style="text-align: center;width:100%">没有搜索到您要查询的数据...</em></li>';
	cw.common.changePage(url,pageSize,xmlname,result,fn_queryT_FIXED_ASSETS_DETAILLIST,pagePanel,defaultHtml);
}
var fn_queryT_FIXED_ASSETS_DETAILLIST = function(obj,result,i){
	var str='<li id="bar_'+obj.ID+'">';
	str+='<em style="width: 4%; text-align: center;"><input type="checkbox" name="check" value="'+obj.ID+'" /></em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.BARCODE+'</em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.FIXED_ASSETS_NAME+'</em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.WITH_MANANGER_NAME+'</em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.USE_MANANGER+'</em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.DEPARTMENT_NO_NAME+'</em>';
	str+='<em style="width: 10%; text-align: center;">'+obj.SUPPLIERS_NO_NAME+'</em>';
	str+='<em style="width: 12%; text-align: center;"><a href="../check/print_godown_entry_detail.html?ID='+obj.ID+'&STATUS=3" target="_blank">打印</a></em>';
	str+='</li>';
	$(result).append(str);
}
//批量删除
function batchDel(){
	if(getCheckedNum('check')==0){
		alert('请选择要删除的资产条码');
		return;
	}
	if(!confirm('确定要删除资产吗？')){
		return;
	}
	var barcodeA = getCheckedVal('check').split(',');
	$.post(cw.common.path + '/json',
		{
			action : 'BATCH_DELETE_T_FIXED_ASSETS_DETAIL_ACTION',
			BARCODE : barcodeA
		}, function(data){
			if (data.success) {
				for(var i=0;i<barcodeA.length;i++)
				{
					$("#bar_"+barcodeA[i]).remove();
				}
				alert('批量删除资产成功');
			} else {
				alert(data.errors.errmsg);
			}
		}, 'json');
}
function subButton(){
	$('#subImg').unbind('click',subButton);
	return jQuery.formValidator.pageIsValid(1);
}

function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){$('#subImg').bind('click',subButton);alert(msg);},onsuccess:function(){subFrm();return false;}});
	
	$("#FIXED_ASSETS_NAME").formValidator({onshow:"请输入资产名称",onfocus:"资产名称由英文或简体中文组成"}).inputValidator({min:1,onerror:"请输入资产名称"}).inputValidator({max:32,onerror:"资产名称过长，长度不超过32个字符"})
	$("#BRAND").formValidator({empty:true,onshow:"请输入品牌型号",oncorrect:"正确",onfocus:"请输入品牌型号"}).inputValidator({min:1,onerror:"请输入品牌型号",max:300,onerror:"字数不超过300个字符"});
	$("#REMARK").formValidator({empty:true,onshow:"请输入用户备注",oncorrect:"正确",onfocus:"请输入用户备注信息"}).inputValidator({max:400,onerror:"用户备注信息中文不超过200个字，英文不超过400个字符"});
	$("#STOCK_DATE").focus(function(){WdatePicker({skin:'whyGreen',oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})}).formValidator({onshow:"请输入的购买日期",onfocus:"请输入购买",oncorrect:"你输入的日期合法"}).inputValidator({min:"1900-01-01",max:"3000-01-01",type:"date",onerror:"日期必须在\"1900-01-01\"和\"3000-01-01\"之间"});
	$("#BEFOR_PRICE").formValidator({empty:true,onshow:"请输入变更前价格",oncorrect:"正确",onfocus:"请输入变更前价格"}).regexValidator({regexp:"num",datatype:"enum",onerror:"您输入的价格不正确"});
	$("#STOCK_PRICE").formValidator({empty:true,onshow:"请输入购买价格",oncorrect:"正确",onfocus:"请输入购买价格"}).regexValidator({regexp:"num",datatype:"enum",onerror:"您输入的价格不正确"});
	$("#USE_YEAR").formValidator({empty:true,onshow:"请输入使用年限",oncorrect:"正确",onfocus:"请输使用年限"}).regexValidator({regexp:"intege1",datatype:"enum",onerror:"您输入的年限不正确"});;
	$("#WARRANTY").formValidator({empty:true,onshow:"请输入保修期",oncorrect:"正确",onfocus:"请输入保修期"}).inputValidator({min:1,onerror:"请输入保修期",max:10,onerror:"字数不超过10个字符"});
	$("#SPECIFICATION").formValidator({empty:true,onshow:"请输入规格",oncorrect:"正确",onfocus:"请输入规格"}).inputValidator({min:1,onerror:"请输入规格",max:14,onerror:"字数不超过14个字符"});
	$("#FACTORY_NO").formValidator({empty:true,onshow:"请输入出厂编号",oncorrect:"正确",onfocus:"请输入出厂编号"}).inputValidator({min:1,onerror:"请输入出厂编号",max:30,onerror:"字数不超过30个字符"});
	$("#STORED_IN").formValidator({empty:true,onshow:"请输入存放地",oncorrect:"正确",onfocus:"请输入存放地"}).inputValidator({min:1,onerror:"请输入存放地",max:30,onerror:"字数不超过30个字符"});
	$("#NUMBER").formValidator({onshow:"请输入数量",oncorrect:"正确",onfocus:"请输入数量"}).regexValidator({regexp:"num1",datatype:"enum",onerror:"您输入的数量不正确"});	
	$("#NCNO").formValidator({empty:true,onshow:"请输入NC编号",oncorrect:"正确",onfocus:"请输入NC编号"}).inputValidator({max:60,onerror:"NC编号中文不超过30个字，英文不超过60个字符"});
	$("#USE_MANANGER").formValidator({empty:true,onshow:"请输入使用 人",oncorrect:"正确",onfocus:"请输入使用 人"}).inputValidator({min:1,onerror:"请输入使用 人",max:30,onerror:"字数不超过30个字符"});
}
//查询资产类型
var FIXED_ASSETS_TYPE = function(){
	cw.common.getDictByPage('FIXED_ASSETS_TYPE','','#FIXED_ASSETS_TYPE',printFIXED_ASSETS_TYPE)
}
var printFIXED_ASSETS_TYPE = function(obj,result,i){
	$(result).append('<option value="'+obj.TYPE_ID+'">'+obj.TYPE_NAME+'</option>');
}

//查询资产性质
var FIXED_ASSETS_NATURE = function(){
	cw.common.getDictByPage('FIXED_ASSETS_NATURE','','#FIXED_ASSETS_NATURE',printFIXED_ASSETS_NATURE)
}
var printFIXED_ASSETS_NATURE = function(obj,result,i){
	$(result).append('<option value="'+obj.TYPE_ID+'">'+obj.TYPE_NAME+'</option>');
}
